<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>所有课程信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100vh;
        }
        .course-list {
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 800px;
            text-align: left;
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 12px;
            border-bottom: 1px solid #ddd;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .error {
            color: red;
            text-align: center;
        }
        .pagination {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-top: 20px;
        }
        .pagination a {
            margin: 0 5px;
            padding: 5px 10px;
            background-color: rgba(0, 123, 255, 0.3);
            color: #007bff;
            border-radius: 5px;
            text-decoration: none;
            transition: background-color 0.3s ease;
            font-size: 14px;
        }
        .pagination a:hover {
            background-color: rgba(0, 123, 255, 0.5);
        }
        .pagination .current-page {
            background-color: rgba(0, 123, 255, 0.7);
            color: #fff;
            cursor: default;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="course-list">
        <h1>所有课程信息</h1>
        {% if error %}
            <p class="error">{{ error }}</p>
        {% elif courses %}
            <table>
                <thead>
                    <tr>
                        <th>课程编号</th>
                        <th>课程名</th>
                        <th>课时</th>
                    </tr>
                </thead>
                <tbody>
                    {% for course in courses %}
                    <tr>
                        <td>{{ course.cid }}</td>
                        <td>{{ course.cname }}</td>
                        <td>{{ course.hour }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>

            <!-- 分页导航 -->
            <div class="pagination">
                <span class="current-page">{{ page }}</span>
                {% if page > 1 %}
                    <a href="{{ url_for('show_courses', page=1) }}">首页</a>
                    <a href="{{ url_for('show_courses', page=page-1) }}">上一页</a>
                {% endif %}

                <!-- {% for p in range(1, total_pages + 1) %}
                    {% if p != page %}
                        <a href="{{ url_for('show_courses', page=p) }}">{{ p }}</a>
                    {% endif %}
                {% endfor %} -->

                {% if total_pages <= 20 %}
                    {% for p in range(1, total_pages + 1) %}
                        {% if p == page %}
                            <strong>{{ p }}</strong>
                        {% else %}
                            <a href="{{ url_for('show_courses', page=p) }}">{{ p }}</a>
                        {% endif %}
                    {% endfor %}
                {% else %}
                    {% if page > 6 %}
                        ...
                    {% endif %}
                    {% for p in range(page - 5, page + 5) %}
                        {% if p > 0 and p <= total_pages %}
                            {% if p == page %}
                                <strong>{{ p }}</strong>
                            {% else %}
                                <a href="{{ url_for('show_courses', page=p) }}">{{ p }}</a>
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                    {% if page < total_pages - 2 %}
                        ...
                        <a href="{{ url_for('show_courses', page=page + 1) }}">下一页</a>
                        <a href="{{ url_for('show_courses', page=total_pages) }}">尾页</a>
                    {% endif %}
                    {% if page < total_pages and page >= total_pages - 2 %}
                        <a href="{{ url_for('show_courses', page=page + 1) }}">下一页</a>
                        <a href="{{ url_for('show_courses', page=total_pages) }}">尾页</a>
                    {% endif %}
                {% endif %}


                <!-- {% if page < total_pages %}
                    <a href="{{ url_for('show_courses', page=page + 1) }}">下一页</a>
                    <a href="{{ url_for('show_courses', page=total_pages) }}">尾页</a>
                {% endif %} -->
            </div>
        {% else %}
            <p class="error">没有获得课程信息</p>
        {% endif %}
        <a href="/" class="back-button">返回</a>
    </div>
</body>
</html>